﻿local LibTweetCraft = LibStub:GetLibrary("LibTweetCraft-1.2")

function TweetCraftFrame_OnTweetsChanged(event)
  if event == "LibTweetCraft_TweetsChanged" and PanelTemplates_GetSelectedTab(TweetCraftFrame) == 1 then
	TweetCraftFrame_UpdateTweets()
  elseif event == "LibTweetCraft_RepliesChanged" and PanelTemplates_GetSelectedTab(TweetCraftFrame) == 2 then
	TweetCraftFrame_UpdateTweets()
  elseif event == "LibTweetCraft_OutgoingTweetsChanged" and PanelTemplates_GetSelectedTab(TweetCraftFrame) == 3 then
	TweetCraftFrame_UpdateTweets()
  end
end

function TweetCraftFrame_OnLoad(self)
  -- Initialize TweetCraft
  TweetCraft:Initialize()
  
  -- Register for the ADDON_LOADED event
  self:RegisterEvent("ADDON_LOADED");

  -- Subscribe to notifications in outgoing tweet changes
  LibTweetCraft.RegisterCallback(self, "LibTweetCraft_TweetsChanged", TweetCraftFrame_OnTweetsChanged)
  LibTweetCraft.RegisterCallback(self, "LibTweetCraft_RepliesChanged", TweetCraftFrame_OnTweetsChanged)
  LibTweetCraft.RegisterCallback(self, "LibTweetCraft_OutgoingTweetsChanged", TweetCraftFrame_OnTweetsChanged)
 
  -- Set UIPanel layout attributes
  TweetCraftFrame:SetAttribute("UIPanelLayout-defined", true)
  TweetCraftFrame:SetAttribute("UIPanelLayout-enabled", true)
  TweetCraftFrame:SetAttribute("UIPanelLayout-area", "left")
  TweetCraftFrame:SetAttribute("UIPanelLayout-pushable", 5)
  -- TweetCraftFrame:SetAttribute("UIPanelLayout-width", 384)
  TweetCraftFrame:SetAttribute("UIPanelLayout-whileDead", true)
end

function TweetCraftFrame_OnEvent(panel, event, ...)
	local arg1 = ...
	
	if event == "ADDON_LOADED" and arg1 == "TweetCraft" then
		-- Check whether a TweetCraft status message is available
		if tweetCraftStatus then
			-- Print the status message
			print(tweetCraftStatus)
		end
		
		-- Call TweetCraft:AddOnLoaded
		TweetCraft:AddOnLoaded();
	end
end

function TweetCraftFrame_SelectedTabChanged(frame)
	TweetCraftFrame_UpdateTweets()
end

function TweetCraftFrame_UpdateTweets()
	local tweets = {}
	if PanelTemplates_GetSelectedTab(TweetCraftFrame) == 1 then
		tweets = LibTweetCraft:GetIncomingTweets()
	elseif PanelTemplates_GetSelectedTab(TweetCraftFrame) == 2 then
		tweets = LibTweetCraft:GetReplies()
	else
		tweets = LibTweetCraft:GetOutgoingTweets()
	end

	-- Update the scroll frame with the number of tweets
	FauxScrollFrame_Update(TweetsScrollFrame, #tweets, 5, 52)
	
	local i, tweetIndex

	for i = 1, 5 do
     local tweetButtonName = "TweetCraftTweetButton" .. i
     tweetIndex = i + FauxScrollFrame_GetOffset(TweetsScrollFrame)
	
	 if (tweetIndex <= #tweets) then
	   local tweet = tweets[tweetIndex]
	   
	   TweetCraftFrame_UpdateTweet(tweetButtonName, tweet)
	   
	   getglobal(tweetButtonName):Show()
	 else
 	   getglobal(tweetButtonName).tweet = nil
	   getglobal(tweetButtonName):Hide()
	 end
	end	
end

function TweetCraftFrame_UpdateTweet(tweetButtonName, tweet)
	   -- Associate the tweet with the tweet button
	   getglobal(tweetButtonName).tweet = tweet

	   -- Set the screen name, message text and date sent (extra type checks)
	   getglobal(tweetButtonName .. "ScreenName"):SetText(tweet.User.ScreenName)
	   getglobal(tweetButtonName .. "Message"):SetText(tweet.Message)
	   getglobal(tweetButtonName .. "DateSent"):SetText(tweet.RelativeDateSent)

	   -- Reset texture as it might not have a valid user picture
       getglobal(tweetButtonName .. "Picture"):SetTexture(nil)
	   
	   -- Determine whether a picture is available for the user
	   if userPictures and userPictures[tweet.User.Id] and type(userPictures[tweet.User.Id]) == "string" then
	      getglobal(tweetButtonName .. "Picture"):SetTexture(userPictures[tweet.User.Id])
	   end
end

function TweetCraftFrame_UpdateButtons()
	if TweetEditBox:GetNumLetters() > 0 then
		TweetCraftQueueButton:Enable()
	else
		TweetCraftQueueButton:Disable()
	end

	if TweetEditBox:GetNumLetters() > 0 or TweetCraft:HasOutgoingTweets() then
		TweetCraftRefreshButton:SetText(SEND)
	else
		TweetCraftRefreshButton:SetText(TWEETCRAFTREFRESH)
	end
end

function TweetCraftFrame_Refresh(self)
  -- If the TweetEditBox is not empty, queue its contents, send it
  if TweetEditBox:GetNumLetters() > 0 then
    TweetCraftFrame_Queue(TweetEditBox)  
    LibTweetCraft:Send()
  end

  -- Refresh the UI (and TweetCraft)
  LibTweetCraft:Refresh()
end

function TweetCraftFrame_Screenshot(self)
	Screenshot()
end

function TweetCraftFrame_Options(self)
	InterfaceOptionsFrame_OpenToCategory(TweetCraftOptionsPanel)
end

function TweetCraftFrame_Queue(self)
	-- Perform a manual tweet using LibTweetCraft with the content of the EditBox
	LibTweetCraft:Tweet(TweetEditBox:GetText())
	
	-- Reset the content of the EditBox
	TweetEditBox:SetText("")
end

function TweetButton_OnClick(self)
	if self.tweet and (self.tweet.type == "IncomingTweet" or self.tweet.type == "Reply") and self.tweet.User.ScreenName then
		TweetEditBox:SetText("@" .. self.tweet.User.ScreenName .. " ")
		TweetEditBox:SetFocus()
	end
end

function TweetButton_OnDelete(tweet)
	LibTweetCraft:RemoveOutgoingTweet(tweet)
	TweetCraftFrame_UpdateButtons()
end

function TweetButton_OnRetweet(tweet)
	-- Check whether the tweet already starts with 'RT'
	if tweet.Message:find("RT", 1, true) then
		TweetEditBox:SetText(tweet.Message:gsub("RT", "RT @" .. tweet.User.ScreenName))
	else
		TweetEditBox:SetText("RT @" .. tweet.User.ScreenName .. " " .. tweet.Message)
	end
end